package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;
import com.wwwarehouse.common.bean.PaymentParamBean;
import com.wwwarehouse.financialcenter.customview.AutoClickButton;
import com.wwwarehouse.financialcenter.eventbus.Event;
import com.wwwarehouse.financialcenter.eventbus.EventHideTitle;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.billingrecords.ChargingRecordDetailsFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.payment.PaymentFragment;

/**
 * @author Joseph on 2017/11/14.
 *         <p/>
 *         账单详情
 */

public class BillDetailsFragment extends TheParentFragment implements ElasticScrollView.OnPullListener {
    private ElasticScrollView mElasticScrollView;

    private ImageView mHeadImg;
    private TextView mNameTxt;
    private TextView mAmountTxt;
    private StateButton mModeBtn;
    private TextView mTypeTxt;
    private TextView mMoneyTxt;
    private TextView mStatusTxt;
    private TextView mDescTxt;
    private TextView mDateTxt;
    private TextView mTimeTxt;
    private StateButton mPayBtn;
    private AutoClickButton mBtnPay;
    private RelativeLayout mOrderAmountLayout;
    private RelativeLayout mOrderDescLayout;
    private BillRecordResponseBean.BillRecordDetailsBean mBillRecordDetailsBean;
    private boolean isFunction  = false; //是否是从功能卡进入
    @Override
    protected int getContentId() {
        return R.layout.financial_center_bill_details_fragment;
    }

    @Override
    public void setTitle() {
        if (peekFragment()instanceof BillDetailsFragment){
            mActivity.setTitle(getString(R.string.financial_center_bill_detail));
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void init(View view) {
        Bundle bundle = getArguments();
        if (bundle!=null){
            mBillRecordDetailsBean = bundle.getParcelable("data");
            isFunction = bundle.getBoolean("isFunction");
        }
        mBtnPay = (AutoClickButton) view.findViewById(R.id.confirm_btn);
        mBtnPay.setEnabled(true);
        mBtnPay.setVisibility(isFunction?View.VISIBLE:View.GONE);
        mBtnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goPayment();
            }
        });

        mHeadImg = (ImageView) view.findViewById(R.id.iv_head);
        mNameTxt = (TextView) view.findViewById(R.id.tv_name);
        mAmountTxt = (TextView) view.findViewById(R.id.tv_amount);
        mModeBtn = (StateButton) view.findViewById(R.id.btn_mode);
        mTypeTxt = (TextView) view.findViewById(R.id.tv_type);
        mMoneyTxt = (TextView) view.findViewById(R.id.tv_money);
        mStatusTxt = (TextView) view.findViewById(R.id.tv_status);
        mDescTxt = (TextView) view.findViewById(R.id.tv_desc);
        mDateTxt = (TextView) view.findViewById(R.id.tv_date);
        mTimeTxt = (TextView) view.findViewById(R.id.tv_time);
        mPayBtn = (StateButton) view.findViewById(R.id.btn_pay);
        mOrderAmountLayout = (RelativeLayout) view.findViewById(R.id.rl_order_amount);
        mOrderDescLayout = (RelativeLayout) view.findViewById(R.id.rl_order_desc);

        mElasticScrollView = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
        mElasticScrollView.setOnPullListener(this);
    }

    @Override
    protected void loadDatas() {
        if (!TextUtils.isEmpty(mBillRecordDetailsBean.getOtherSideLogo())) {
            BaseApplication.getApplicationInstance().displayImg(mBillRecordDetailsBean.getOtherSideLogo(), mHeadImg);
        }

        mNameTxt.setText(mBillRecordDetailsBean.getOtherSideName());
        mAmountTxt.setText(mBillRecordDetailsBean.getBillAmount().replace("+", "").replace("-", ""));
        mModeBtn.setText(mBillRecordDetailsBean.getPayTypeDesc());
        mTypeTxt.setText(mBillRecordDetailsBean.getBillTypeDesc());

        if ("in".equals(mBillRecordDetailsBean.getInOrOut())) {
            mMoneyTxt.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
            mMoneyTxt.setText(mBillRecordDetailsBean.getBillAmount());
        } else if ("out".equals(mBillRecordDetailsBean.getInOrOut())) {
            mMoneyTxt.setTextColor(mActivity.getResources().getColor(R.color.common_color_c2_f7a82f));
            mMoneyTxt.setText(mBillRecordDetailsBean.getBillAmount());
        } else {

        }

        mStatusTxt.setText(mBillRecordDetailsBean.getPaySatusDesc());

        if ("1".equals(mBillRecordDetailsBean.getPaySatus())) {
            // 未出账
            mStatusTxt.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
        } else if ("2".equals(mBillRecordDetailsBean.getPaySatus())) {
            // 未收款/未付款
            if ("1".equals(mBillRecordDetailsBean.getIsOverdue())) {
                //逾期
                mStatusTxt.setTextColor(mActivity.getResources().getColor(R.color.common_color_c11_ff1f1f));
            } else {
                mStatusTxt.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
            }
        } else if ("4".equals(mBillRecordDetailsBean.getPaySatus())) {
            // 已收款/已付款
        } else {

        }

        mDescTxt.setText(mBillRecordDetailsBean.getBillDetail());
        mDateTxt.setText(mBillRecordDetailsBean.getPlanPayTime());
        mTimeTxt.setText(mBillRecordDetailsBean.getFinishTime());

        mOrderAmountLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 查询账单关联的计费记录
                Bundle bundle = new Bundle();
                bundle.putParcelable("details", mBillRecordDetailsBean);

                ChargingRecordDetailsFragment chargingRecordDetailsFragment = new ChargingRecordDetailsFragment();
                chargingRecordDetailsFragment.setArguments(bundle);
                pushFragment(chargingRecordDetailsFragment);
            }
        });

        mOrderDescLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 新接口
                Bundle bundle = new Bundle();
                bundle.putParcelable("details", mBillRecordDetailsBean);

                BillOrderGoodsPagerFragment billOrderGoodsPagerFragment = new BillOrderGoodsPagerFragment();
                billOrderGoodsPagerFragment.setArguments(bundle);
                pushFragment(billOrderGoodsPagerFragment);
            }
        });
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }
    //支付成功之后刷新状态
    @Override
    protected void receiveEvent(Event event) {
        if (event.getCode()== EventHideTitle.CODE.B&&event.getData() instanceof EventHideTitle){
            if (isFunction){
                popFragment();
            }else {
                isFunction = true;
                mStatusTxt.setText(R.string.financial_center_pay_already);
                mStatusTxt.setTextColor(getResources().getColor(R.color.common_color_c7_96999e));
            }
        }
    }
    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onUpPull() {
        if (isFunction)
            return;
        // 未收款/未付款
        if ("2".equals(mBillRecordDetailsBean.getPaySatus()) && isShow) {
            mPayBtn.setVisibility(View.VISIBLE);

            mPayBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goPayment();
                }
            });
        } else {
            mPayBtn.setVisibility(View.GONE);
        }

        isShow = !isShow;
    }

    private void goPayment() {
        PaymentParamBean paymentParamBean = new PaymentParamBean();
        paymentParamBean.setOtherSideName(mBillRecordDetailsBean.getOtherSideName());
        paymentParamBean.setBillType(mBillRecordDetailsBean.getBillType());
        paymentParamBean.setBillAmount(mBillRecordDetailsBean.getBillAmount());
        paymentParamBean.setBillTradeNo(mBillRecordDetailsBean.getBillUkid());

        Bundle bundle = new Bundle();
        bundle.putSerializable("payment_param", paymentParamBean);

        PaymentFragment paymentFragment = new PaymentFragment();
        paymentFragment.setArguments(bundle);
        pushFragment(paymentFragment);
    }
}
